<script setup lang="ts">
import { computed } from "vue";
// Button组件接收不同的props 为表现不同的组件样式
const props = defineProps({
  theme: { type: String, default: "button" },
  size: { type: String, default: "normal" },
  level: { type: String, default: "normal" },
  disabled: { type: Boolean, default: false },
  radius: { type: Boolean, default: false },
  loading: { type: Boolean, default: false },
});

// const classes = computed(() => {
//   return {
//     [`szy-theme-${props.theme}`]: props.theme,
//     [`szy-size-${props.size}`]: props.size,
//     [`szy-level-${props.level}`]: props.level,
//   };
// });

// 使用props收到的属性拼接一个字符串数组 这个数组会作为组件的属性名
const classes = computed(() => {
  return [
    `szy-theme-${props.theme}`,
    `szy-size-${props.size}`,
    `szy-level-${props.level}`,
  ];
});
</script>

<template>
  <button
    class="szy-button"
    :class="classes"
    :disabled="props.disabled"
    :radius="props.radius"
  >
    <span v-if="loading" class="szy-loadingIndicator"></span>
    <slot></slot>
  </button>
</template>

<style lang="scss" scoped>
// scss常量
$h: 40px;
$border-color: #d9d9d9;
$color: #333;
$blue: #40a9ff;
$radius: 4px;
$red: #f56c6c;
$grey: #909399;
$green: #67c23a;
$origin: #e6a23c;
$bluelit: #ecf5ff;
.szy-button {
  box-sizing: border-box;
  height: $h;
  padding: 0 15px;
  cursor: pointer;
  display: inline-flex;
  justify-content: center;
  align-items: center;
  white-space: nowrap;
  background: white;
  line-height: 90%;
  color: $color;
  border: 1px solid $border-color;
  border-radius: $radius;
  box-shadow: 0 1px 0 fade-out(black, 0.95);
  transition: background 250ms;
  margin-right: 20px;
  & + & {
    margin-left: 8px;
  }
  // scss中如果需要写伪类比如:hover 就需要在伪类前写& 相当于把选择器拼接到:hover之前
  &:hover,
  &:focus {
    color: $blue;
    border-color: $blue;
    background-color: $bluelit;
  }
  // scss中如果需要写伪类比如:hover 就需要在伪类前写& 相当于把选择器拼接到:hover之前
  &:focus {
    outline: none;
  }
  &::-moz-focus-inner {
    border: 0;
  }

  &.szy-theme-link {
    border-color: transparent;
    box-shadow: none;
    color: $blue;
    &:hover,
    &:focus {
      color: lighten($blue, 10%);
    }
  }
  &.szy-theme-text {
    border-color: transparent;
    box-shadow: none;
    color: inherit;
    // scss中如果需要写伪类比如:hover 就需要在伪类前写& 相当于把选择器拼接到:hover之前
    &:hover,
    &:focus {
      background: darken(white, 5%);
    }
  }

  &.szy-size-big {
    font-size: 24px;
    height: 48px;
    padding: 0 16px;
  }
  &.szy-size-small {
    font-size: 12px;
    height: 20px;
    padding: 0 4px;
  }

  &.szy-theme-button {
    &[disabled] {
      color: white;
    }
    &.szy-level-main {
      background-color: $blue;
      color: #fff;
      border-color: $blue;
      &:hover,
      &:focus {
        background-color: lighten($blue, 10%);
        border-color: lighten($blue, 10%);
      }
    }
    &.szy-level-succese {
      background-color: $green;
      color: #fff;
      border-color: $green;
      &:hover,
      &:focus {
        background-color: lighten($green, 10%);
        border-color: lighten($green, 10%);
      }
    }
    &.szy-level-info {
      background-color: $grey;
      color: #fff;
      border-color: $grey;
      &:hover,
      &:focus {
        background-color: lighten($grey, 10%);
        border-color: lighten($grey, 10%);
      }
    }
    &.szy-level-warrning {
      background-color: $origin;
      color: #fff;
      border-color: $origin;
      &:hover,
      &:focus {
        background-color: lighten($origin, 10%);
        border-color: lighten($origin, 10%);
      }
    }
    &.szy-level-danger {
      background-color: $red;
      color: #fff;
      border-color: $red;
      &:hover,
      &:focus {
        background-color: lighten($red, 10%);
        border-color: lighten($red, 10%);
      }
    }
  }
  &.szy-theme-link {
    &.szy-level-danger {
      color: $red;
      &:hover,
      &:focus {
        color: darken($red, 10%);
      }
    }
  }
  &.szy-theme-text {
    &.szy-level-main {
      color: $blue;
      &:hover,
      &:focus {
        color: darken($blue, 10%);
      }
    }
    &.szy-level-danger {
      color: $red;
      &:hover,
      &:focus {
        color: darken($red, 10%);
      }
    }
  }
  &.szy-theme-button {
    &[radius="true"] {
      border-radius: 20px;
    }
  }
  &.szy-theme-button {
    &[disabled] {
      cursor: not-allowed;
      color: $grey;
      &:hover {
        border-color: $grey;
      }
    }
  }
  &.szy-level-main,
  &.szy-level-succese,
  &.szy-level-info,
  &.szy-level-warrning,
  &.szy-level-danger {
    &[disabled] {
      color: white;
      opacity: 0.5;
    }
  }
  &.szy-theme-link,
  &.szy-theme-text {
    &[disabled] {
      cursor: not-allowed;
      color: $grey;
    }
  }
  > .szy-loadingIndicator {
    width: 14px;
    height: 14px;
    display: inline-block;
    margin-right: 4px;
    border-radius: 8px;
    // 实现旋转圆圈的原理是圆角制造一个原 然后让其中一个border设置transparent 这样就留下一个四分之三圆
    border-color: $blue $blue $blue transparent;
    border-style: solid;
    border-width: 2px;
    // 过度动画 szy-spin 动画持续时间1S 无限循环动画 动画执行为线性
    animation: szy-spin 1s infinite linear;
  }
}
// 关键帧动画 szy-spin的不同阶段执行
@keyframes szy-spin {
  // 执行旋转
  0% {
    transform: rotate(0deg);
  }
  100% {
    transform: rotate(360deg);
  }
}
</style>
